//
//  GoodsController.m
//  MindBox
//
//  Created by MS on 16-1-15.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "GoodsController.h"
#import "GSAdsView.h"
#import "MBProgressHUD+MJ.h"
#import "Discover_MindPageTool.h"
#import "Goods_Product_View.h"
#import "NSString+Components.h"
#import "UIImageView+WebCache.h"
#import "UIImage+MultiFormat.h"
@interface GoodsController ()<UIScrollViewDelegate>
@property (nonatomic,weak) UIView * markView;
@property (nonatomic,weak) MyNavigationBar * navBar;
@property (nonatomic,weak) UIView * alView;
@property (nonatomic,weak) UIScrollView * scrollView;
@property (nonatomic,strong) GoodsDetailModel * goodsDetailModel;
@property (nonatomic,weak) GSAdsView * adsView;
@property (nonatomic,weak) Goods_Product_View * productView;
@property (nonatomic,assign) CGRect imageViewRect;
@end

@implementation GoodsController

-(GoodsDetailModel *)goodsDetailModel
{
    if (_goodsDetailModel==nil) {
        _goodsDetailModel =  [[GoodsDetailModel alloc] init];
    }
    return _goodsDetailModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addScrollView];
    [self setNavBar];
    
    
    [self loadGoodsData];
    
}

-(void)loadGoodsData
{
    [MBProgressHUD showMessage:@"正在加载数据..."];
    [Discover_MindPageTool mindBannerDataWithParam:self.good_id Success:^(GoodsDetailModel *data) {
        
        [MBProgressHUD hideHUD];
        
        NSString * imgStr =data.my_description;
        
        NSArray * newStrAray =[NSString stringWithStr:imgStr ComponentStr:@"src" startSubStr:@"http" endSubStr:@".jpg"];
        
        data.images_url_descrip = newStrAray;
        self.goodsDetailModel = data;
        
        self.adsView.imagesArray = data.rotation_images;
        self.productView.goodsDetailData = data;
        
        [self addImageViewsWithArray:newStrAray];
        
        [self.scrollView setNeedsLayout];
        [self.scrollView layoutIfNeeded];
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)addImageViewsWithArray:(NSArray *)images_url
{
    
    self.imageViewRect = CGRectMake(0, CGRectGetMaxY(_productView.frame), self.view.width, 0);
    
    for (int i =0; i<images_url.count; i++) {
        
        UIImageView * imgeV = [[UIImageView alloc] init];
        
        [self.scrollView addSubview:imgeV];
        [imgeV sd_setImageWithURL:images_url[i]  completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            if (image.size.width) {
                 CGFloat tmp = self.view.width/image.size.width;
                imgeV.frame = CGRectMake(0, CGRectGetMaxY(self.imageViewRect), self.view.width, tmp * image.size.height);
            }
            
            self.imageViewRect = imgeV.frame;
            if (i==images_url.count-1) {
                self.scrollView.contentSize = CGSizeMake(0,CGRectGetMaxY(_imageViewRect));
            }

        }];
    }
    
}


-(void)addScrollView
{
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:scrollView];
    scrollView.contentSize = CGSizeMake(0, 2000);
    
    //添加广告条
    GSAdsView * adsView = [[GSAdsView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.width)];
    [scrollView addSubview:adsView];
    adsView.pageTintColor = [UIColor whiteColor];
    adsView.currentPageTintColor = [UIColor redColor];
    adsView.intervalTime = 5;
    self.adsView = adsView;
    
    //添加介绍栏
    Goods_Product_View * productView = [Goods_Product_View goods_product_ViewWithXib];
    productView.frame = CGRectMake(0, CGRectGetMaxY(adsView.frame), self.view.width, 265);
    [scrollView addSubview:productView];
    self.productView = productView;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat NewY =scrollView.contentOffset.y;
    if (NewY<=200) {
        self.alView.alpha = NewY/200;
        self.markView.alpha = NewY/200;
    }
    
    else{
        self.alView.alpha = 1;
        self.markView.alpha = 1;
    }
    
    self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.imageViewRect));
}


-(void)setNavBar
{
    MyNavigationItem * leftItem = [MyNavigationItem navigationItemWithImage:@"ic_bg_white_arrow_left" andHighlightImage:@"ic_bg_white_arrow_left" andTag:0 andTitle:nil addTarget:self action:@selector(leftBtnClick)];
    
    MyNavigationItem * rightItem =[MyNavigationItem navigationItemWithImage:@"ic_bg_white_more" andHighlightImage:@"ic_bg_white_more" andTag:0 andTitle:nil addTarget:self action:@selector(rightClick)];
    MyNavigationBar * navBar = [[MyNavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.width, 44)];
    self.navBar = navBar;
    
    UIView * view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [navBar addSubview:view];
    view.frame = navBar.bounds;
    self.alView = view;
    view.alpha = 0;

    for (UIView * view in self.tabBarController.view.subviews) {
        NSString * viewName = NSStringFromClass([view class]);
        if ([viewName isEqualToString:@"UIView"]) {
            view.alpha = 0;
            self.markView = view;
        }
    }
    
    [navBar setNavigationBarWithBgImageName:@"无" andTitle:nil  andTitleView:nil andLeftItems:@[leftItem] andRightItems:@[rightItem]];
    [self.view addSubview:navBar];
    navBar.backgroundColor = [UIColor clearColor];
    
}

-(void)leftBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightClick
{
    
}



-(void)dealloc
{
    self.markView.alpha = 1;
    
//    NSLog(@"GoodsController卸载");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
